
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/routing/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>URL Routing &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex/index.html" />
    <link rel="search" title="Search" href="../search/index.html" />
    <link rel="next" title="WSGI Helpers" href="../wsgi/index.html" />
    <link rel="prev" title="Request / Response Objects" href="../wrappers/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "routing"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../wsgi/index.html" title="WSGI Helpers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../wrappers/index.html" title="Request / Response Objects"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-werkzeug.routing">
<span id="url-routing"></span><span id="routing"></span><h1>URL Routing<a class="headerlink" href="#module-werkzeug.routing" title="Permalink to this headline">¶</a></h1>
<p>When it comes to combining multiple controller or view functions (however
you want to call them), you need a dispatcher.  A simple way would be
applying regular expression tests on <code class="docutils literal notranslate"><span class="pre">PATH_INFO</span></code> and call registered
callback functions that return the value.</p>
<p>Werkzeug provides a much more powerful system, similar to <a class="reference external" href="https://routes.readthedocs.io/en/latest/">Routes</a>.  All the
objects mentioned on this page must be imported from <a class="reference internal" href="#module-werkzeug.routing" title="werkzeug.routing"><code class="xref py py-mod docutils literal notranslate"><span class="pre">werkzeug.routing</span></code></a>, not
from <code class="xref py py-mod docutils literal notranslate"><span class="pre">werkzeug</span></code>!</p>
<div class="section" id="quickstart">
<h2>Quickstart<a class="headerlink" href="#quickstart" title="Permalink to this headline">¶</a></h2>
<p>Here is a simple example which could be the URL definition for a blog:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="k">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">NotFound</span><span class="p">,</span> <span class="n">RequestRedirect</span>

<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/index&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;int:year&gt;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/archive&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;int:year&gt;/&lt;int:month&gt;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/archive&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;int:year&gt;/&lt;int:month&gt;/&lt;int:day&gt;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/archive&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;int:year&gt;/&lt;int:month&gt;/&lt;int:day&gt;/&lt;slug&gt;&#39;</span><span class="p">,</span>
         <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/show_post&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/about&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/about_me&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/feeds/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/feeds&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/feeds/&lt;feed_name&gt;.rss&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/show_feed&#39;</span><span class="p">)</span>
<span class="p">])</span>

<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">urls</span> <span class="o">=</span> <span class="n">url_map</span><span class="o">.</span><span class="n">bind_to_environ</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">endpoint</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">()</span>
    <span class="k">except</span> <span class="n">HTTPException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">e</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
    <span class="n">start_response</span><span class="p">(</span><span class="s1">&#39;200 OK&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain&#39;</span><span class="p">)])</span>
    <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;Rule points to </span><span class="si">%r</span><span class="s1"> with arguments </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">args</span><span class="p">)]</span>
</pre></div>
</div>
<p>So what does that do?  First of all we create a new <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a> which stores
a bunch of URL rules.  Then we pass it a list of <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rule</span></code></a> objects.</p>
<p>Each <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rule</span></code></a> object is instantiated with a string that represents a rule
and an endpoint which will be the alias for what view the rule represents.
Multiple rules can have the same endpoint, but should have different arguments
to allow URL construction.</p>
<p>The format for the URL rules is straightforward, but explained in detail below.</p>
<p>Inside the WSGI application we bind the url_map to the current request which will
return a new <a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">MapAdapter</span></code></a>.  This url_map adapter can then be used to match
or build domains for the current request.</p>
<p>The <a class="reference internal" href="#werkzeug.routing.MapAdapter.match" title="werkzeug.routing.MapAdapter.match"><code class="xref py py-meth docutils literal notranslate"><span class="pre">MapAdapter.match()</span></code></a> method can then either return a tuple in the form
<code class="docutils literal notranslate"><span class="pre">(endpoint,</span> <span class="pre">args)</span></code> or raise one of the three exceptions
<a class="reference internal" href="../exceptions/index.html#werkzeug.exceptions.NotFound" title="werkzeug.exceptions.NotFound"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotFound</span></code></a>, <a class="reference internal" href="../exceptions/index.html#werkzeug.exceptions.MethodNotAllowed" title="werkzeug.exceptions.MethodNotAllowed"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MethodNotAllowed</span></code></a>,
or <code class="xref py py-exc docutils literal notranslate"><span class="pre">RequestRedirect</span></code>.  For more details about those
exceptions have a look at the documentation of the <a class="reference internal" href="#werkzeug.routing.MapAdapter.match" title="werkzeug.routing.MapAdapter.match"><code class="xref py py-meth docutils literal notranslate"><span class="pre">MapAdapter.match()</span></code></a> method.</p>
</div>
<div class="section" id="rule-format">
<h2>Rule Format<a class="headerlink" href="#rule-format" title="Permalink to this headline">¶</a></h2>
<p>Rule strings are URL paths with placeholders for variable parts in the
format <code class="docutils literal notranslate"><span class="pre">&lt;converter(arguments):name&gt;</span></code>. <code class="docutils literal notranslate"><span class="pre">converter</span></code> and <code class="docutils literal notranslate"><span class="pre">arguments</span></code>
(with parentheses) are optional. If no converter is given, the
<code class="docutils literal notranslate"><span class="pre">default</span></code> converter is used (<code class="docutils literal notranslate"><span class="pre">string</span></code> by default). The available
converters are discussed below.</p>
<p>Rules that end with a slash are “branches”, others are “leaves”. If
<code class="docutils literal notranslate"><span class="pre">strict_slashes</span></code> is enabled (the default), visiting a branch URL
without a trailing slash will redirect to the URL with a slash appended.</p>
<p>Many HTTP servers merge consecutive slashes into one when receiving
requests. If <code class="docutils literal notranslate"><span class="pre">merge_slashes</span></code> is enabled (the default), rules will
merge slashes in non-variable parts when matching and building. Visiting
a URL with consecutive slashes will redirect to the URL with slashes
merged. If you want to disable <code class="docutils literal notranslate"><span class="pre">merge_slashes</span></code> for a <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rule</span></code></a> or
<a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>, you’ll also need to configure your web server
appropriately.</p>
</div>
<div class="section" id="built-in-converters">
<h2>Built-in Converters<a class="headerlink" href="#built-in-converters" title="Permalink to this headline">¶</a></h2>
<p>Converters for common types of URL variables are built-in. The available
converters can be overridden or extended through <a class="reference internal" href="#werkzeug.routing.Map.converters" title="werkzeug.routing.Map.converters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Map.converters</span></code></a>.</p>
<dl class="class">
<dt id="werkzeug.routing.UnicodeConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">UnicodeConverter</code><span class="sig-paren">(</span><em>map</em>, <em>minlength=1</em>, <em>maxlength=None</em>, <em>length=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.UnicodeConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter is the default converter and accepts any string but
only one path segment.  Thus the string can not include a slash.</p>
<p>This is the default validator.</p>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/pages/&lt;page&gt;&#39;</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;string(length=2):lang_code&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</li>
<li><strong>minlength</strong> – the minimum length of the string.  Must be greater
or equal 1.</li>
<li><strong>maxlength</strong> – the maximum length of the string.</li>
<li><strong>length</strong> – the exact length of the string.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.PathConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">PathConverter</code><span class="sig-paren">(</span><em>map</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.PathConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>Like the default <a class="reference internal" href="#werkzeug.routing.UnicodeConverter" title="werkzeug.routing.UnicodeConverter"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnicodeConverter</span></code></a>, but it also matches
slashes.  This is useful for wikis and similar applications:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;path:wikipage&gt;&#39;</span><span class="p">)</span>
<span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;path:wikipage&gt;/edit&#39;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.AnyConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">AnyConverter</code><span class="sig-paren">(</span><em>map</em>, <em>*items</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.AnyConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>Matches one of the items provided.  Items can either be Python
identifiers or strings:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&lt;any(about, help, imprint, class, &quot;foo,bar&quot;):page_name&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</li>
<li><strong>items</strong> – this function accepts the possible items as positional
arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.IntegerConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">IntegerConverter</code><span class="sig-paren">(</span><em>map</em>, <em>fixed_digits=0</em>, <em>min=None</em>, <em>max=None</em>, <em>signed=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.IntegerConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter only accepts integer values:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/page/&lt;int:page&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>By default it only accepts unsigned, positive values. The <code class="docutils literal notranslate"><span class="pre">signed</span></code>
parameter will enable signed, negative values.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/page/&lt;int(signed=True):page&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – The <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</li>
<li><strong>fixed_digits</strong> – The number of fixed digits in the URL. If you
set this to <code class="docutils literal notranslate"><span class="pre">4</span></code> for example, the rule will only match if the
URL looks like <code class="docutils literal notranslate"><span class="pre">/0001/</span></code>. The default is variable length.</li>
<li><strong>min</strong> – The minimal value.</li>
<li><strong>max</strong> – The maximal value.</li>
<li><strong>signed</strong> – Allow signed (negative) values.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.15: </span>The <code class="docutils literal notranslate"><span class="pre">signed</span></code> parameter.</p>
</div>
</details></dd></dl>

<dl class="class">
<dt id="werkzeug.routing.FloatConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">FloatConverter</code><span class="sig-paren">(</span><em>map</em>, <em>min=None</em>, <em>max=None</em>, <em>signed=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.FloatConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter only accepts floating point values:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/probability/&lt;float:probability&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>By default it only accepts unsigned, positive values. The <code class="docutils literal notranslate"><span class="pre">signed</span></code>
parameter will enable signed, negative values.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/offset/&lt;float(signed=True):offset&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – The <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</li>
<li><strong>min</strong> – The minimal value.</li>
<li><strong>max</strong> – The maximal value.</li>
<li><strong>signed</strong> – Allow signed (negative) values.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.15: </span>The <code class="docutils literal notranslate"><span class="pre">signed</span></code> parameter.</p>
</div>
</details></dd></dl>

<dl class="class">
<dt id="werkzeug.routing.UUIDConverter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">UUIDConverter</code><span class="sig-paren">(</span><em>map</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.UUIDConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter only accepts UUID strings:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/object/&lt;uuid:identifier&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.10.</span></p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>.</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="maps-rules-and-adapters">
<h2>Maps, Rules and Adapters<a class="headerlink" href="#maps-rules-and-adapters" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.Map">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">Map</code><span class="sig-paren">(</span><em>rules=None</em>, <em>default_subdomain=''</em>, <em>charset='utf-8'</em>, <em>strict_slashes=True</em>, <em>merge_slashes=True</em>, <em>redirect_defaults=True</em>, <em>converters=None</em>, <em>sort_parameters=False</em>, <em>sort_key=None</em>, <em>encoding_errors='replace'</em>, <em>host_matching=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map" title="Permalink to this definition">¶</a></dt>
<dd><p>The map class stores all the URL rules and some configuration
parameters.  Some of the configuration values are only stored on the
<cite>Map</cite> instance since those affect all rules, others are just defaults
and can be overridden for each rule.  Note that you have to specify all
arguments besides the <cite>rules</cite> as keyword arguments!</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>rules</strong> – sequence of url rules for this map.</li>
<li><strong>default_subdomain</strong> – The default subdomain for rules without a
subdomain defined.</li>
<li><strong>charset</strong> – charset of the url. defaults to <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code></li>
<li><strong>strict_slashes</strong> – If a rule ends with a slash but the matched
URL does not, redirect to the URL with a trailing slash.</li>
<li><strong>merge_slashes</strong> – Merge consecutive slashes when matching or
building URLs. Matches will redirect to the normalized URL.
Slashes in variable parts are not merged.</li>
<li><strong>redirect_defaults</strong> – This will redirect to the default rule if it
wasn’t visited that way. This helps creating
unique URLs.</li>
<li><strong>converters</strong> – A dict of converters that adds additional converters
to the list of converters. If you redefine one
converter this will override the original one.</li>
<li><strong>sort_parameters</strong> – If set to <cite>True</cite> the url parameters are sorted.
See <cite>url_encode</cite> for more details.</li>
<li><strong>sort_key</strong> – The sort key function for <cite>url_encode</cite>.</li>
<li><strong>encoding_errors</strong> – the error method to use for decoding</li>
<li><strong>host_matching</strong> – if set to <cite>True</cite> it enables the host matching
feature and disables the subdomain one.  If
enabled the <cite>host</cite> parameter to rules is used
instead of the <cite>subdomain</cite> one.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0: </span>If <code class="docutils literal notranslate"><span class="pre">url_scheme</span></code> is <code class="docutils literal notranslate"><span class="pre">ws</span></code> or <code class="docutils literal notranslate"><span class="pre">wss</span></code>, only WebSocket rules
will match.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0: </span>Added <code class="docutils literal notranslate"><span class="pre">merge_slashes</span></code>.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.7: </span>Added <code class="docutils literal notranslate"><span class="pre">encoding_errors</span></code> and <code class="docutils literal notranslate"><span class="pre">host_matching</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5: </span>Added <code class="docutils literal notranslate"><span class="pre">sort_parameters</span></code> and <code class="docutils literal notranslate"><span class="pre">sort_key</span></code>.</p>
</div>
</details><dl class="attribute">
<dt id="werkzeug.routing.Map.converters">
<code class="descname">converters</code><a class="headerlink" href="#werkzeug.routing.Map.converters" title="Permalink to this definition">¶</a></dt>
<dd><p>The dictionary of converters.  This can be modified after the class
was created, but will only affect rules added after the
modification.  If the rules are defined with the list passed to the
class, the <cite>converters</cite> parameter to the constructor has to be used
instead.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.add">
<code class="descname">add</code><span class="sig-paren">(</span><em>rulefactory</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a new rule or factory to the map and bind it.  Requires that the
rule is not bound to another map.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>rulefactory</strong> – a <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rule</span></code></a> or <a class="reference internal" href="#werkzeug.routing.RuleFactory" title="werkzeug.routing.RuleFactory"><code class="xref py py-class docutils literal notranslate"><span class="pre">RuleFactory</span></code></a></td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.bind">
<code class="descname">bind</code><span class="sig-paren">(</span><em>server_name</em>, <em>script_name=None</em>, <em>subdomain=None</em>, <em>url_scheme='http'</em>, <em>default_method='GET'</em>, <em>path_info=None</em>, <em>query_args=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.bind" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">MapAdapter</span></code></a> with the details specified to the
call.  Note that <cite>script_name</cite> will default to <code class="docutils literal notranslate"><span class="pre">'/'</span></code> if not further
specified or <cite>None</cite>.  The <cite>server_name</cite> at least is a requirement
because the HTTP RFC requires absolute URLs for redirects and so all
redirect exceptions raised by Werkzeug will contain the full canonical
URL.</p>
<p>If no path_info is passed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">match()</span></code> it will use the default path
info passed to bind.  While this doesn’t really make sense for
manual bind calls, it’s useful if you bind a map to a WSGI
environment which already contains the path info.</p>
<p><cite>subdomain</cite> will default to the <cite>default_subdomain</cite> for this map if
no defined. If there is no <cite>default_subdomain</cite> you cannot use the
subdomain feature.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0: </span>If <code class="docutils literal notranslate"><span class="pre">url_scheme</span></code> is <code class="docutils literal notranslate"><span class="pre">ws</span></code> or <code class="docutils literal notranslate"><span class="pre">wss</span></code>, only WebSocket rules
will match.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.15: </span><code class="docutils literal notranslate"><span class="pre">path_info</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">'/'</span></code> if <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span><code class="docutils literal notranslate"><span class="pre">query_args</span></code> can be a string.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.7: </span>Added <code class="docutils literal notranslate"><span class="pre">query_args</span></code>.</p>
</div>
</details></dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.bind_to_environ">
<code class="descname">bind_to_environ</code><span class="sig-paren">(</span><em>environ</em>, <em>server_name=None</em>, <em>subdomain=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.bind_to_environ" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#werkzeug.routing.Map.bind" title="werkzeug.routing.Map.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bind()</span></code></a> but you can pass it an WSGI environment and it
will fetch the information from that dictionary.  Note that because of
limitations in the protocol there is no way to get the current
subdomain and real <cite>server_name</cite> from the environment.  If you don’t
provide it, Werkzeug will use <cite>SERVER_NAME</cite> and <cite>SERVER_PORT</cite> (or
<cite>HTTP_HOST</cite> if provided) as used <cite>server_name</cite> with disabled subdomain
feature.</p>
<p>If <cite>subdomain</cite> is <cite>None</cite> but an environment and a server name is
provided it will calculate the current subdomain automatically.
Example: <cite>server_name</cite> is <code class="docutils literal notranslate"><span class="pre">'example.com'</span></code> and the <cite>SERVER_NAME</cite>
in the wsgi <cite>environ</cite> is <code class="docutils literal notranslate"><span class="pre">'staging.dev.example.com'</span></code> the calculated
subdomain will be <code class="docutils literal notranslate"><span class="pre">'staging.dev'</span></code>.</p>
<p>If the object passed as environ has an environ attribute, the value of
this attribute is used instead.  This allows you to pass request
objects.  Additionally <cite>PATH_INFO</cite> added as a default of the
<a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">MapAdapter</span></code></a> so that you don’t have to pass the path info to
the match method.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0.0: </span>If the passed server name specifies port 443, it will match
if the incoming scheme is <code class="docutils literal notranslate"><span class="pre">https</span></code> without a port.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0.0: </span>A warning is shown when the passed server name does not
match the incoming WSGI server name.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span>This will no longer raise a ValueError when an unexpected server
name was passed.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5: </span>previously this method accepted a bogus <cite>calculate_subdomain</cite>
parameter that did not have any effect.  It was removed because
of that.</p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – a WSGI environment.</li>
<li><strong>server_name</strong> – an optional server name hint (see above).</li>
<li><strong>subdomain</strong> – optionally the current subdomain (see above).</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="werkzeug.routing.Map.default_converters">
<code class="descname">default_converters</code><em class="property"> = {'any': &lt;class 'werkzeug.routing.AnyConverter'&gt;, 'default': &lt;class 'werkzeug.routing.UnicodeConverter'&gt;, 'float': &lt;class 'werkzeug.routing.FloatConverter'&gt;, 'int': &lt;class 'werkzeug.routing.IntegerConverter'&gt;, 'path': &lt;class 'werkzeug.routing.PathConverter'&gt;, 'string': &lt;class 'werkzeug.routing.UnicodeConverter'&gt;, 'uuid': &lt;class 'werkzeug.routing.UUIDConverter'&gt;}</em><a class="headerlink" href="#werkzeug.routing.Map.default_converters" title="Permalink to this definition">¶</a></dt>
<dd><p>A dict of default converters to be used.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.is_endpoint_expecting">
<code class="descname">is_endpoint_expecting</code><span class="sig-paren">(</span><em>endpoint</em>, <em>*arguments</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.is_endpoint_expecting" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over all rules and check if the endpoint expects
the arguments provided.  This is for example useful if you have
some URLs that expect a language code and others that do not and
you want to wrap the builder a bit so that the current language
code is automatically added if not provided but endpoints expect
it.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>endpoint</strong> – the endpoint to check.</li>
<li><strong>arguments</strong> – this function accepts one or more arguments
as positional arguments.  Each one of them is
checked.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.iter_rules">
<code class="descname">iter_rules</code><span class="sig-paren">(</span><em>endpoint=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.iter_rules" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over all rules or the rules of an endpoint.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>endpoint</strong> – if provided only the rules for that endpoint
are returned.</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">an iterator</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.lock_class">
<code class="descname">lock_class</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.lock_class" title="Permalink to this definition">¶</a></dt>
<dd><p>The type of lock to use when updating.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.Map.update">
<code class="descname">update</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Map.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Called before matching and building to keep the compiled rules
in the correct order after things changed.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.MapAdapter">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">MapAdapter</code><span class="sig-paren">(</span><em>map</em>, <em>server_name</em>, <em>script_name</em>, <em>subdomain</em>, <em>url_scheme</em>, <em>path_info</em>, <em>default_method</em>, <em>query_args=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returned by <a class="reference internal" href="#werkzeug.routing.Map.bind" title="werkzeug.routing.Map.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Map.bind()</span></code></a> or <a class="reference internal" href="#werkzeug.routing.Map.bind_to_environ" title="werkzeug.routing.Map.bind_to_environ"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Map.bind_to_environ()</span></code></a> and does
the URL matching and building based on runtime information.</p>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.allowed_methods">
<code class="descname">allowed_methods</code><span class="sig-paren">(</span><em>path_info=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.allowed_methods" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the valid methods that match for a given path.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</details></dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.build">
<code class="descname">build</code><span class="sig-paren">(</span><em>endpoint</em>, <em>values=None</em>, <em>method=None</em>, <em>force_external=False</em>, <em>append_unknown=True</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.build" title="Permalink to this definition">¶</a></dt>
<dd><p>Building URLs works pretty much the other way round.  Instead of
<cite>match</cite> you call <cite>build</cite> and pass it the endpoint and a dict of
arguments for the placeholders.</p>
<p>The <cite>build</cite> function also accepts an argument called <cite>force_external</cite>
which, if you set it to <cite>True</cite> will force external URLs. Per default
external URLs (include the server name) will only be used if the
target URL is on a different subdomain.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/downloads/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;downloads/index&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/downloads/&lt;int:id&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;downloads/show&#39;</span><span class="p">)</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s2">&quot;example.com&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;index&quot;</span><span class="p">,</span> <span class="p">{})</span>
<span class="go">&#39;/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;downloads/show&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="mi">42</span><span class="p">})</span>
<span class="go">&#39;/downloads/42&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;downloads/show&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="mi">42</span><span class="p">},</span> <span class="n">force_external</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">&#39;http://example.com/downloads/42&#39;</span>
</pre></div>
</div>
<p>Because URLs cannot contain non ASCII data you will always get
bytestrings back.  Non ASCII characters are urlencoded with the
charset defined on the map instance.</p>
<p>Additional values are converted to unicode and appended to the URL as
URL querystring parameters:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;index&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;q&#39;</span><span class="p">:</span> <span class="s1">&#39;My Searchstring&#39;</span><span class="p">})</span>
<span class="go">&#39;/?q=My+Searchstring&#39;</span>
</pre></div>
</div>
<p>When processing those additional values, lists are furthermore
interpreted as multiple values (as per
<a class="reference internal" href="../datastructures/index.html#werkzeug.datastructures.MultiDict" title="werkzeug.datastructures.MultiDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">werkzeug.datastructures.MultiDict</span></code></a>):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;index&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;q&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]})</span>
<span class="go">&#39;/?q=a&amp;q=b&amp;q=c&#39;</span>
</pre></div>
</div>
<p>Passing a <code class="docutils literal notranslate"><span class="pre">MultiDict</span></code> will also add multiple values:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;index&quot;</span><span class="p">,</span> <span class="n">MultiDict</span><span class="p">(((</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="s1">&#39;z&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;q&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;q&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">))))</span>
<span class="go">&#39;/?p=z&amp;q=a&amp;q=b&#39;</span>
</pre></div>
</div>
<p>If a rule does not exist when building a <cite>BuildError</cite> exception is
raised.</p>
<p>The build method accepts an argument called <cite>method</cite> which allows you
to specify the method you want to have an URL built for if you have
different methods for the same endpoint specified.</p>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>the <cite>append_unknown</cite> parameter was added.</p>
</div>
</details><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>endpoint</strong> – the endpoint of the URL to build.</li>
<li><strong>values</strong> – the values for the URL to build.  Unhandled values are
appended to the URL as query parameters.</li>
<li><strong>method</strong> – the HTTP method for the rule if there are different
URLs for different methods on the same endpoint.</li>
<li><strong>force_external</strong> – enforce full canonical external URLs. If the URL
scheme is not provided, this will generate
a protocol-relative URL.</li>
<li><strong>append_unknown</strong> – unknown parameters are appended to the generated
URL as query string argument.  Disable this
if you want the builder to ignore those.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.dispatch">
<code class="descname">dispatch</code><span class="sig-paren">(</span><em>view_func</em>, <em>path_info=None</em>, <em>method=None</em>, <em>catch_http_exceptions=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.dispatch" title="Permalink to this definition">¶</a></dt>
<dd><p>Does the complete dispatching process.  <cite>view_func</cite> is called with
the endpoint and a dict with the values for the view.  It should
look up the view function, call it, and return a response object
or WSGI application.  http exceptions are not caught by default
so that applications can display nicer error messages by just
catching them by hand.  If you want to stick with the default
error messages you can pass it <code class="docutils literal notranslate"><span class="pre">catch_http_exceptions=True</span></code> and
it will catch the http exceptions.</p>
<p>Here a small example for the dispatch usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="k">import</span> <span class="n">Request</span><span class="p">,</span> <span class="n">Response</span>
<span class="kn">from</span> <span class="nn">werkzeug.wsgi</span> <span class="k">import</span> <span class="n">responder</span>
<span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="k">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span>

<span class="k">def</span> <span class="nf">on_index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s1">&#39;Hello from the index&#39;</span><span class="p">)</span>

<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span><span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">)])</span>
<span class="n">views</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;index&#39;</span><span class="p">:</span> <span class="n">on_index</span><span class="p">}</span>

<span class="nd">@responder</span>
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
    <span class="n">urls</span> <span class="o">=</span> <span class="n">url_map</span><span class="o">.</span><span class="n">bind_to_environ</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">urls</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="k">lambda</span> <span class="n">e</span><span class="p">,</span> <span class="n">v</span><span class="p">:</span> <span class="n">views</span><span class="p">[</span><span class="n">e</span><span class="p">](</span><span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">v</span><span class="p">),</span>
                         <span class="n">catch_http_exceptions</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Keep in mind that this method might return exception objects, too, so
use <code class="xref py py-class docutils literal notranslate"><span class="pre">Response.force_type</span></code> to get a response object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>view_func</strong> – a function that is called with the endpoint as
first argument and the value dict as second.  Has
to dispatch to the actual view function with this
information.  (see above)</li>
<li><strong>path_info</strong> – the path info to use for matching.  Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching.  Overrides the
method specified on binding.</li>
<li><strong>catch_http_exceptions</strong> – set to <cite>True</cite> to catch any of the
werkzeug <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPException</span></code>s.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.get_host">
<code class="descname">get_host</code><span class="sig-paren">(</span><em>domain_part</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.get_host" title="Permalink to this definition">¶</a></dt>
<dd><p>Figures out the full host name for the given domain part.  The
domain part is a subdomain in case host matching is disabled or
a full host name.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.make_alias_redirect_url">
<code class="descname">make_alias_redirect_url</code><span class="sig-paren">(</span><em>path</em>, <em>endpoint</em>, <em>values</em>, <em>method</em>, <em>query_args</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.make_alias_redirect_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Internally called to make an alias redirect URL.</p>
</dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.match">
<code class="descname">match</code><span class="sig-paren">(</span><em>path_info=None</em>, <em>method=None</em>, <em>return_rule=False</em>, <em>query_args=None</em>, <em>websocket=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.match" title="Permalink to this definition">¶</a></dt>
<dd><p>The usage is simple: you just pass the match method the current
path info as well as the method (which defaults to <cite>GET</cite>).  The
following things can then happen:</p>
<ul class="simple">
<li>you receive a <cite>NotFound</cite> exception that indicates that no URL is
matching.  A <cite>NotFound</cite> exception is also a WSGI application you
can call to get a default page not found page (happens to be the
same object as <cite>werkzeug.exceptions.NotFound</cite>)</li>
<li>you receive a <cite>MethodNotAllowed</cite> exception that indicates that there
is a match for this URL but not for the current request method.
This is useful for RESTful applications.</li>
<li>you receive a <cite>RequestRedirect</cite> exception with a <cite>new_url</cite>
attribute.  This exception is used to notify you about a request
Werkzeug requests from your WSGI application.  This is for example the
case if you request <code class="docutils literal notranslate"><span class="pre">/foo</span></code> although the correct URL is <code class="docutils literal notranslate"><span class="pre">/foo/</span></code>
You can use the <cite>RequestRedirect</cite> instance as response-like object
similar to all other subclasses of <cite>HTTPException</cite>.</li>
<li>you receive a <code class="docutils literal notranslate"><span class="pre">WebsocketMismatch</span></code> exception if the only
match is a WebSocket rule but the bind is an HTTP request, or
if the match is an HTTP rule but the bind is a WebSocket
request.</li>
<li>you get a tuple in the form <code class="docutils literal notranslate"><span class="pre">(endpoint,</span> <span class="pre">arguments)</span></code> if there is
a match (unless <cite>return_rule</cite> is True, in which case you get a tuple
in the form <code class="docutils literal notranslate"><span class="pre">(rule,</span> <span class="pre">arguments)</span></code>)</li>
</ul>
<p>If the path info is not passed to the match method the default path
info of the map is used (defaults to the root URL if not defined
explicitly).</p>
<p>All of the exceptions raised are subclasses of <cite>HTTPException</cite> so they
can be used as WSGI responses. They will all render generic error or
redirect pages.</p>
<p>Here is a small example for matching:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/downloads/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;downloads/index&#39;</span><span class="p">),</span>
<span class="gp">... </span>    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/downloads/&lt;int:id&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;downloads/show&#39;</span><span class="p">)</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s2">&quot;example.com&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">,</span> <span class="s2">&quot;GET&quot;</span><span class="p">)</span>
<span class="go">(&#39;index&#39;, {})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;/downloads/42&quot;</span><span class="p">)</span>
<span class="go">(&#39;downloads/show&#39;, {&#39;id&#39;: 42})</span>
</pre></div>
</div>
<p>And here is what happens on redirect and missing URLs:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;/downloads&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">RequestRedirect</span>: <span class="n">http://example.com/downloads/</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;/missing&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">NotFound</span>: <span class="n">404 Not Found</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>path_info</strong> – the path info to use for matching.  Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching.  Overrides the
method specified on binding.</li>
<li><strong>return_rule</strong> – return the rule that matched instead of just the
endpoint (defaults to <cite>False</cite>).</li>
<li><strong>query_args</strong> – optional query arguments that are used for
automatic redirects as string or dictionary.  It’s
currently not possible to use the query arguments
for URL matching.</li>
<li><strong>websocket</strong> – Match WebSocket instead of HTTP requests. A
websocket request has a <code class="docutils literal notranslate"><span class="pre">ws</span></code> or <code class="docutils literal notranslate"><span class="pre">wss</span></code>
<code class="xref py py-attr docutils literal notranslate"><span class="pre">url_scheme</span></code>. This overrides that detection.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0: </span>Added <code class="docutils literal notranslate"><span class="pre">websocket</span></code>.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span><code class="docutils literal notranslate"><span class="pre">query_args</span></code> can be a string.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span>Added <code class="docutils literal notranslate"><span class="pre">query_args</span></code>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>Added <code class="docutils literal notranslate"><span class="pre">return_rule</span></code>.</p>
</div>
</details></dd></dl>

<dl class="method">
<dt id="werkzeug.routing.MapAdapter.test">
<code class="descname">test</code><span class="sig-paren">(</span><em>path_info=None</em>, <em>method=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.MapAdapter.test" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if a rule would match.  Works like <cite>match</cite> but returns <cite>True</cite>
if the URL matches, or <cite>False</cite> if it does not exist.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>path_info</strong> – the path info to use for matching.  Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching.  Overrides the
method specified on binding.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.Rule">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">Rule</code><span class="sig-paren">(</span><em>string</em>, <em>defaults=None</em>, <em>subdomain=None</em>, <em>methods=None</em>, <em>build_only=False</em>, <em>endpoint=None</em>, <em>strict_slashes=None</em>, <em>merge_slashes=None</em>, <em>redirect_to=None</em>, <em>alias=False</em>, <em>host=None</em>, <em>websocket=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Rule" title="Permalink to this definition">¶</a></dt>
<dd><p>A Rule represents one URL pattern.  There are some options for <cite>Rule</cite>
that change the way it behaves and are passed to the <cite>Rule</cite> constructor.
Note that besides the rule-string all arguments <em>must</em> be keyword arguments
in order to not break the application on Werkzeug upgrades.</p>
<dl class="docutils">
<dt><cite>string</cite></dt>
<dd><p class="first">Rule strings basically are just normal URL paths with placeholders in
the format <code class="docutils literal notranslate"><span class="pre">&lt;converter(arguments):name&gt;</span></code> where the converter and the
arguments are optional.  If no converter is defined the <cite>default</cite>
converter is used which means <cite>string</cite> in the normal configuration.</p>
<p>URL rules that end with a slash are branch URLs, others are leaves.
If you have <cite>strict_slashes</cite> enabled (which is the default), all
branch URLs that are matched without a trailing slash will trigger a
redirect to the same URL with the missing slash appended.</p>
<p class="last">The converters are defined on the <cite>Map</cite>.</p>
</dd>
<dt><cite>endpoint</cite></dt>
<dd>The endpoint for this rule. This can be anything. A reference to a
function, a string, a number etc.  The preferred way is using a string
because the endpoint is used for URL generation.</dd>
<dt><cite>defaults</cite></dt>
<dd><p class="first">An optional dict with defaults for other rules with the same endpoint.
This is a bit tricky but useful if you want to have unique URLs:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/all/&#39;</span><span class="p">,</span> <span class="n">defaults</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;page&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;all_entries&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/all/page/&lt;int:page&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;all_entries&#39;</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
<p class="last">If a user now visits <code class="docutils literal notranslate"><span class="pre">http://example.com/all/page/1</span></code> he will be
redirected to <code class="docutils literal notranslate"><span class="pre">http://example.com/all/</span></code>.  If <cite>redirect_defaults</cite> is
disabled on the <cite>Map</cite> instance this will only affect the URL
generation.</p>
</dd>
<dt><cite>subdomain</cite></dt>
<dd><p class="first">The subdomain rule string for this rule. If not specified the rule
only matches for the <cite>default_subdomain</cite> of the map.  If the map is
not bound to a subdomain this feature is disabled.</p>
<p>Can be useful if you want to have user profiles on different subdomains
and all subdomains are forwarded to your application:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">subdomain</span><span class="o">=</span><span class="s1">&#39;&lt;username&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;user/homepage&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/stats&#39;</span><span class="p">,</span> <span class="n">subdomain</span><span class="o">=</span><span class="s1">&#39;&lt;username&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;user/stats&#39;</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
</dd>
<dt><cite>methods</cite></dt>
<dd>A sequence of http methods this rule applies to.  If not specified, all
methods are allowed. For example this can be useful if you want different
endpoints for <cite>POST</cite> and <cite>GET</cite>.  If methods are defined and the path
matches but the method matched against is not in this list or in the
list of another rule for that path the error raised is of the type
<cite>MethodNotAllowed</cite> rather than <cite>NotFound</cite>.  If <cite>GET</cite> is present in the
list of methods and <cite>HEAD</cite> is not, <cite>HEAD</cite> is added automatically.</dd>
<dt><cite>strict_slashes</cite></dt>
<dd>Override the <cite>Map</cite> setting for <cite>strict_slashes</cite> only for this rule. If
not specified the <cite>Map</cite> setting is used.</dd>
<dt><cite>merge_slashes</cite></dt>
<dd>Override <code class="xref py py-attr docutils literal notranslate"><span class="pre">Map.merge_slashes</span></code> for this rule.</dd>
<dt><cite>build_only</cite></dt>
<dd>Set this to True and the rule will never match but will create a URL
that can be build. This is useful if you have resources on a subdomain
or folder that are not handled by the WSGI application (like static data)</dd>
<dt><cite>redirect_to</cite></dt>
<dd><p class="first">If given this must be either a string or callable.  In case of a
callable it’s called with the url adapter that triggered the match and
the values of the URL as keyword arguments and has to return the target
for the redirect, otherwise it has to be a string with placeholders in
rule syntax:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo_with_slug</span><span class="p">(</span><span class="n">adapter</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
    <span class="c1"># ask the database for the slug for the old id.  this of</span>
    <span class="c1"># course has nothing to do with werkzeug.</span>
    <span class="k">return</span> <span class="s1">&#39;foo/&#39;</span> <span class="o">+</span> <span class="n">Foo</span><span class="o">.</span><span class="n">get_slug_for_id</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>

<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/foo/&lt;slug&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/some/old/url/&lt;slug&gt;&#39;</span><span class="p">,</span> <span class="n">redirect_to</span><span class="o">=</span><span class="s1">&#39;foo/&lt;slug&gt;&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/other/old/url/&lt;int:id&gt;&#39;</span><span class="p">,</span> <span class="n">redirect_to</span><span class="o">=</span><span class="n">foo_with_slug</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
<p>When the rule is matched the routing system will raise a
<cite>RequestRedirect</cite> exception with the target for the redirect.</p>
<p class="last">Keep in mind that the URL will be joined against the URL root of the
script so don’t use a leading slash on the target URL unless you
really mean root of that domain.</p>
</dd>
<dt><cite>alias</cite></dt>
<dd>If enabled this rule serves as an alias for another rule with the same
endpoint and arguments.</dd>
<dt><cite>host</cite></dt>
<dd>If provided and the URL map has host matching enabled this can be
used to provide a match rule for the whole host.  This also means
that the subdomain feature is disabled.</dd>
<dt><cite>websocket</cite></dt>
<dd>If <code class="docutils literal notranslate"><span class="pre">True</span></code>, this rule is only matches for WebSocket (<code class="docutils literal notranslate"><span class="pre">ws://</span></code>,
<code class="docutils literal notranslate"><span class="pre">wss://</span></code>) requests. By default, rules will only match for HTTP
requests.</dd>
</dl>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0: </span>Added <code class="docutils literal notranslate"><span class="pre">websocket</span></code>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0: </span>Added <code class="docutils literal notranslate"><span class="pre">merge_slashes</span></code>.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span>Added <code class="docutils literal notranslate"><span class="pre">alias</span></code> and <code class="docutils literal notranslate"><span class="pre">host</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.6.1: </span><code class="docutils literal notranslate"><span class="pre">HEAD</span></code> is added to <code class="docutils literal notranslate"><span class="pre">methods</span></code> if <code class="docutils literal notranslate"><span class="pre">GET</span></code> is present.</p>
</div>
</details><dl class="method">
<dt id="werkzeug.routing.Rule.empty">
<code class="descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Rule.empty" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an unbound copy of this rule.</p>
<p>This can be useful if want to reuse an already bound URL for another
map.  See <code class="docutils literal notranslate"><span class="pre">get_empty_kwargs</span></code> to override what keyword arguments are
provided to the new copy.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="rule-factories">
<h2>Rule Factories<a class="headerlink" href="#rule-factories" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.RuleFactory">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">RuleFactory</code><a class="headerlink" href="#werkzeug.routing.RuleFactory" title="Permalink to this definition">¶</a></dt>
<dd><p>As soon as you have more complex URL setups it’s a good idea to use rule
factories to avoid repetitive tasks.  Some of them are builtin, others can
be added by subclassing <cite>RuleFactory</cite> and overriding <cite>get_rules</cite>.</p>
<dl class="method">
<dt id="werkzeug.routing.RuleFactory.get_rules">
<code class="descname">get_rules</code><span class="sig-paren">(</span><em>map</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.RuleFactory.get_rules" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclasses of <cite>RuleFactory</cite> have to override this method and return
an iterable of rules.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.Subdomain">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">Subdomain</code><span class="sig-paren">(</span><em>subdomain</em>, <em>rules</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Subdomain" title="Permalink to this definition">¶</a></dt>
<dd><p>All URLs provided by this factory have the subdomain set to a
specific domain. For example if you want to use the subdomain for
the current language this can be a good setup:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;#select_language&#39;</span><span class="p">),</span>
    <span class="n">Subdomain</span><span class="p">(</span><span class="s1">&#39;&lt;string(length=2):lang_code&gt;&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/about&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;about&#39;</span><span class="p">),</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/help&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;help&#39;</span><span class="p">)</span>
    <span class="p">])</span>
<span class="p">])</span>
</pre></div>
</div>
<p>All the rules except for the <code class="docutils literal notranslate"><span class="pre">'#select_language'</span></code> endpoint will now
listen on a two letter long subdomain that holds the language code
for the current request.</p>
</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.Submount">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">Submount</code><span class="sig-paren">(</span><em>path</em>, <em>rules</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.Submount" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <cite>Subdomain</cite> but prefixes the URL rule with a given string:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">Submount</span><span class="p">(</span><span class="s1">&#39;/blog&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/index&#39;</span><span class="p">),</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/entry/&lt;entry_slug&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;blog/show&#39;</span><span class="p">)</span>
    <span class="p">])</span>
<span class="p">])</span>
</pre></div>
</div>
<p>Now the rule <code class="docutils literal notranslate"><span class="pre">'blog/show'</span></code> matches <code class="docutils literal notranslate"><span class="pre">/blog/entry/&lt;entry_slug&gt;</span></code>.</p>
</dd></dl>

<dl class="class">
<dt id="werkzeug.routing.EndpointPrefix">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">EndpointPrefix</code><span class="sig-paren">(</span><em>prefix</em>, <em>rules</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.EndpointPrefix" title="Permalink to this definition">¶</a></dt>
<dd><p>Prefixes all endpoints (which must be strings for this factory) with
another string. This can be useful for sub applications:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="n">EndpointPrefix</span><span class="p">(</span><span class="s1">&#39;blog/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">Submount</span><span class="p">(</span><span class="s1">&#39;/blog&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
        <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/entry/&lt;entry_slug&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;show&#39;</span><span class="p">)</span>
    <span class="p">])])</span>
<span class="p">])</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="rule-templates">
<h2>Rule Templates<a class="headerlink" href="#rule-templates" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.RuleTemplate">
<em class="property">class </em><code class="descclassname">werkzeug.routing.</code><code class="descname">RuleTemplate</code><span class="sig-paren">(</span><em>rules</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.routing.RuleTemplate" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns copies of the rules wrapped and expands string templates in
the endpoint, rule, defaults or subdomain sections.</p>
<p>Here a small example for such a rule template:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="k">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">RuleTemplate</span>

<span class="n">resource</span> <span class="o">=</span> <span class="n">RuleTemplate</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/$name/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;$name.list&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/$name/&lt;int:id&gt;&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;$name.show&#39;</span><span class="p">)</span>
<span class="p">])</span>

<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span><span class="n">resource</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;user&#39;</span><span class="p">),</span> <span class="n">resource</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;page&#39;</span><span class="p">)])</span>
</pre></div>
</div>
<p>When a rule template is called the keyword arguments are used to
replace the placeholders in all the string parameters.</p>
</dd></dl>

</div>
<div class="section" id="custom-converters">
<h2>Custom Converters<a class="headerlink" href="#custom-converters" title="Permalink to this headline">¶</a></h2>
<p>You can add custom converters that add behaviors not provided by the
built-in converters. To make a custom converter, subclass
<code class="xref py py-class docutils literal notranslate"><span class="pre">BaseConverter</span></code> then pass the new class to the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a>
<code class="docutils literal notranslate"><span class="pre">converters</span></code> parameter, or add it to
<a class="reference internal" href="#werkzeug.routing.Map.converters" title="werkzeug.routing.Map.converters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">url_map.converters</span></code></a>.</p>
<p>The converter should have a <code class="docutils literal notranslate"><span class="pre">regex</span></code> attribute with a regular
expression to match with. If the converter can take arguments in a URL
rule, it should accept them in its <code class="docutils literal notranslate"><span class="pre">__init__</span></code> method.</p>
<p>It can implement a <code class="docutils literal notranslate"><span class="pre">to_python</span></code> method to convert the matched string to
some other object. This can also do extra validation that wasn’t
possible with the <code class="docutils literal notranslate"><span class="pre">regex</span></code> attribute, and should raise a
<code class="xref py py-exc docutils literal notranslate"><span class="pre">werkzeug.routing.ValidationError</span></code> in that case. Raising any other
errors will cause a 500 error.</p>
<p>It can implement a <code class="docutils literal notranslate"><span class="pre">to_url</span></code> method to convert a Python object to a
string when building a URL. Any error raised here will be converted to a
<code class="xref py py-exc docutils literal notranslate"><span class="pre">werkzeug.routing.BuildError</span></code> and eventually cause a 500 error.</p>
<p>This example implements a <code class="docutils literal notranslate"><span class="pre">BooleanConverter</span></code> that will match the
strings <code class="docutils literal notranslate"><span class="pre">&quot;yes&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;no&quot;</span></code>, and <code class="docutils literal notranslate"><span class="pre">&quot;maybe&quot;</span></code>, returning a random value
for <code class="docutils literal notranslate"><span class="pre">&quot;maybe&quot;</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">random</span> <span class="k">import</span> <span class="n">randrange</span>
<span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="k">import</span> <span class="n">BaseConverter</span><span class="p">,</span> <span class="n">ValidationError</span>

<span class="k">class</span> <span class="nc">BooleanConverter</span><span class="p">(</span><span class="n">BaseConverter</span><span class="p">):</span>
    <span class="n">regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">&quot;(?:yes|no|maybe)&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url_map</span><span class="p">,</span> <span class="n">maybe</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BooleanConverter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">url_map</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">maybe</span> <span class="o">=</span> <span class="n">maybe</span>

    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="s2">&quot;maybe&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">maybe</span><span class="p">:</span>
                <span class="k">return</span> <span class="ow">not</span> <span class="n">randrange</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
            <span class="k">raise</span> <span class="n">ValidationError</span>
        <span class="k">return</span> <span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;yes&#39;</span>

    <span class="k">def</span> <span class="nf">to_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;yes&quot;</span> <span class="k">if</span> <span class="n">value</span> <span class="k">else</span> <span class="s2">&quot;no&quot;</span>

<span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="k">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span>

<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/vote/&lt;bool:werkzeug_rocks&gt;&quot;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;vote&quot;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/guess/&lt;bool(maybe=True):foo&gt;&quot;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;guess&quot;</span><span class="p">)</span>
<span class="p">],</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;bool&#39;</span><span class="p">:</span> <span class="n">BooleanConverter</span><span class="p">})</span>
</pre></div>
</div>
<p>If you want to change the default converter, assign a different
converter to the <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> key.</p>
</div>
<div class="section" id="host-matching">
<h2>Host Matching<a class="headerlink" href="#host-matching" title="Permalink to this headline">¶</a></h2>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</details><p>Starting with Werkzeug 0.7 it’s also possible to do matching on the whole
host names instead of just the subdomain.  To enable this feature you need
to pass <code class="docutils literal notranslate"><span class="pre">host_matching=True</span></code> to the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a> constructor and provide
the <cite>host</cite> argument to all routes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;www_index&#39;</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">&#39;www.example.com&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;help_index&#39;</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">&#39;help.example.com&#39;</span><span class="p">)</span>
<span class="p">],</span> <span class="n">host_matching</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Variable parts are of course also possible in the host section:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;www_index&#39;</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">&#39;www.example.com&#39;</span><span class="p">),</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s1">&#39;user_index&#39;</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">&#39;&lt;user&gt;.example.com&#39;</span><span class="p">)</span>
<span class="p">],</span> <span class="n">host_matching</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="websockets">
<h2>WebSockets<a class="headerlink" href="#websockets" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0.</span></p>
</div>
<p>If a <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rule</span></code></a> is created with <code class="docutils literal notranslate"><span class="pre">websocket=True</span></code>, it will only
match if the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><code class="xref py py-class docutils literal notranslate"><span class="pre">Map</span></code></a> is bound to a request with a <code class="docutils literal notranslate"><span class="pre">url_scheme</span></code> of
<code class="docutils literal notranslate"><span class="pre">ws</span></code> or <code class="docutils literal notranslate"><span class="pre">wss</span></code>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Werkzeug has no further WebSocket support beyond routing. This
functionality is mostly of use to ASGI projects.</p>
</div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
    <span class="n">Rule</span><span class="p">(</span><span class="s2">&quot;/ws&quot;</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;comm&quot;</span><span class="p">,</span> <span class="n">websocket</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
<span class="p">])</span>
<span class="n">adapter</span> <span class="o">=</span> <span class="nb">map</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s2">&quot;example.org&quot;</span><span class="p">,</span> <span class="s2">&quot;/ws&quot;</span><span class="p">,</span> <span class="n">url_scheme</span><span class="o">=</span><span class="s2">&quot;ws&quot;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">adapter</span><span class="o">.</span><span class="n">match</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="s2">&quot;comm&quot;</span><span class="p">,</span> <span class="p">{})</span>
</pre></div>
</div>
<p>If the only match is a WebSocket rule and the bind is HTTP (or the
only match is HTTP and the bind is WebSocket) a
<code class="xref py py-exc docutils literal notranslate"><span class="pre">WebsocketMismatch</span></code> (derives from
<a class="reference internal" href="../exceptions/index.html#werkzeug.exceptions.BadRequest" title="werkzeug.exceptions.BadRequest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadRequest</span></code></a>) exception is raised.</p>
<p>As WebSocket URLs have a different scheme, rules are always built with a
scheme and host, <code class="docutils literal notranslate"><span class="pre">force_external=True</span></code> is implied.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">url</span> <span class="o">=</span> <span class="n">adapter</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s2">&quot;comm&quot;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">url</span> <span class="o">==</span> <span class="s2">&quot;ws://example.org/ws&quot;</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">URL Routing</a><ul>
<li><a class="reference internal" href="#quickstart">Quickstart</a></li>
<li><a class="reference internal" href="#rule-format">Rule Format</a></li>
<li><a class="reference internal" href="#built-in-converters">Built-in Converters</a></li>
<li><a class="reference internal" href="#maps-rules-and-adapters">Maps, Rules and Adapters</a></li>
<li><a class="reference internal" href="#rule-factories">Rule Factories</a></li>
<li><a class="reference internal" href="#rule-templates">Rule Templates</a></li>
<li><a class="reference internal" href="#custom-converters">Custom Converters</a></li>
<li><a class="reference internal" href="#host-matching">Host Matching</a></li>
<li><a class="reference internal" href="#websockets">WebSockets</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../index.html">Overview</a>
    <ul>
          <li>Previous: <a href="../wrappers/index.html" title="previous chapter">Request / Response Objects</a>
          <li>Next: <a href="../wsgi/index.html" title="next chapter">WSGI Helpers</a>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/routing/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>